package com.cloud.common;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


public class LoginInterceptor extends HandlerInterceptorAdapter{

	/**
     * action之前执行
     */
    @Override 
    public boolean preHandle(HttpServletRequest request, 
            HttpServletResponse response, Object handler) throws Exception { 
 
        request.setCharacterEncoding("UTF-8"); 
        response.setCharacterEncoding("UTF-8"); 
        response.setContentType("text/html;charset=UTF-8"); 
 
        // 后台session控制 
        String[] noFilters = new String[] { "login", "static" ,"mobile"};    //url中如果有login不拦截
        String uri = request.getRequestURI();
 
        /*if (uri.indexOf("theme") != -1) { 
            boolean beFilter = true; 
            for (String s : noFilters) { 
                if (uri.indexOf(s) != -1) { 
                    beFilter = false; 
                    break; 
                }
            } 
            if (beFilter) { 
                Object obj = request.getSession().getAttribute(SystemConstants.LOGINED); 
                if (null == obj) { 
                    // 未登录 
                    PrintWriter out = response.getWriter(); 
                    StringBuilder builder = new StringBuilder(); 
                    builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">"); 
                    builder.append("alert(\"页面过期，请重新登录\");"); 
                    builder.append("window.top.location.href=\""); 
                    builder.append(SystemConstants.BASEPATH);  //这里是http://ip:port/项目名
                    builder.append("/login.jsp\";</script>");  //这里是重新登录的页面url
                    out.print(builder.toString()); 
                    out.close(); 
                    return false; 
                }
            } 
        }*/ 
        return super.preHandle(request, response, handler); 
    } 
     
    /**
     * 生成视图之前执行
     */
    @Override 
    public void postHandle(HttpServletRequest request, 
            HttpServletResponse response, Object handler, 
            ModelAndView modelAndView) throws Exception { 
    } 
     
    /**
     * 最后执行，可用于释放资源
     */
    @Override 
    public void afterCompletion(HttpServletRequest request, 
            HttpServletResponse response, Object handler, Exception ex) 
            throws Exception { 
    }  
}
